package com.moji.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.moji.common.base.ApiResult;
import com.moji.entity.MjAboutUs;
import com.moji.entity.MjFlowConfig;
import com.moji.entity.MjNews;
import com.moji.entity.dto.QueryFlowConfigDto;
import com.moji.entity.dto.QueryNewsDto;
import com.moji.service.MjFlowConfigService;
import com.moji.service.MjNewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 新闻中心表 前端控制器
 * </p>
 *
 * @author ccl
 * @since 2024-07-28
 */
@RestController
@RequestMapping("/api/mjNews")
@Api(tags = "新闻中心api相关接口", value = "新闻中心api相关接口")
public class MjNewsController {

    @Resource
    private MjNewsService mjNewsService;

    @PostMapping("/add")
    @ApiOperation(value = "新闻中心_新增",response = Boolean.class)
    public ApiResult add(@Validated @RequestBody MjNews mjNews){
        return ApiResult.ok(mjNewsService.add(mjNews));
    }

    @PostMapping("/edit")
    @ApiOperation(value = "新闻中心_编辑",response = Boolean.class)
    public ApiResult edit(@Validated @RequestBody MjNews mjNews){
        return ApiResult.ok(mjNewsService.edit(mjNews));
    }

    @GetMapping("/delete")
    @ApiOperation(value = "新闻中心_删除",response = Boolean.class)
    public ApiResult delete(@RequestParam("id") Long id){
        return ApiResult.ok(mjNewsService.delete(id));
    }

    @PostMapping("/queryByPage")
    @ApiOperation(value = "新闻中心_分页查询",response = MjAboutUs.class)
    public ApiResult queryByPage(@RequestBody QueryNewsDto dto){
        return ApiResult.ok(mjNewsService.queryByPage(dto));
    }

    @GetMapping("/detail")
    @ApiOperation(value = "新闻中心_详情",response = MjNews.class)
    public ApiResult detail(@RequestParam("id") Long id){
        return ApiResult.ok(mjNewsService.detail(id));
    }
    @PostMapping("/search")
    @ApiOperation(value = "新闻搜索",response = MjNews.class)
    public ApiResult<List<MjNews>> queryByPage(@RequestBody String title){
        LambdaQueryWrapper<MjNews> wrapper = new LambdaQueryWrapper<>();
        //创建时间倒叙
        wrapper.like(MjNews::getTitle,title);
        wrapper.orderByDesc(MjNews::getCreateTime);
        List<MjNews> mjNewsList = mjNewsService.list(wrapper);
        return ApiResult.ok(mjNewsList);
    }
}

